// pages/Home/VehicleSubmit/VehicleSubmit.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryTravelVehicleDetail , queryTravelVehicleSafeConfig , queryTravelVehicleStoreDetails } from '../../../api/vehicle';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    item: {},
    store: {},
    province: '',
    city: '',
    country: '',
    town: '',
    detailedAddress: '',
    rentCarStartCurrent: '',
    rentCarStartYear: '',
    rentCarEndYear: '',
    rentCarStartDate: '', // 开始时间的日期
    rentCarStartWeek: '', // 开始时间的星期
    rentCarStartTime: '', // 开始租车时间
    rentCarDateDiff: '', // 租还间隔时间
    rentCarEndCurrent: '',
    rentCarEndDate: '', // 还车时间的日期
    rentCarEndWeek: '', // 还车时间的星期
    rentCarEndTime: '', // 还车时间
    infoCont: [], // 基本信息
    configCont: [], // 配置信息
    price: 0, // 租车总价
    priceConfig: {},
    showScheduleFee: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.item){
      var item = JSON.parse(options.item);
      var store = JSON.parse(options.store);
      console.log(item,'item')
      console.log(store,'store')
      this.setData({
        item: item,
        store: store,
        province: options.province,
        city: options.city,
        country: options.country,
        town: options.town,
        detailedAddress: options.detailedAddress,
        rentCarStartDate: wx.getStorageSync('rentCarStartDate'),
        rentCarStartTime: wx.getStorageSync('rentCarStartTime'),
        rentCarStartWeek: wx.getStorageSync('rentCarStartWeek'),
        rentCarEndDate: wx.getStorageSync('rentCarEndDate'),
        rentCarEndTime: wx.getStorageSync('rentCarEndTime'),
        rentCarDateDiff: wx.getStorageSync('rentCarDateDiff'),
        rentCarEndWeek: wx.getStorageSync('rentCarEndWeek'),
        rentCarStartCurrent: wx.getStorageSync('rentCarStartCurrent'),
        rentCarEndCurrent: wx.getStorageSync('rentCarEndCurrent'),
        rentCarStartYear: wx.getStorageSync('rentCarStartYear'),
        rentCarEndYear: wx.getStorageSync('rentCarEndYear'),
      })
      wx.setNavigationBarTitle({
        title: store.name,
      })
      this._queryTravelVehicleDetail({type: '1'})
      this._queryTravelVehicleStoreDetails({});
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 获取车辆基本信息
  _queryTravelVehicleDetail({type}){
    let _this = this;
    queryTravelVehicleDetail({vehicleId: _this.data.item.id,type: type})
    .then(res => {
      if(res.success){
        if(type == '0'){
          _this.setData({
            infoCont: res.data.content
          })
        }else{
          let arr = [];
          for (let index = 0; index < res.data.content.length; index++) {
            const element = res.data.content[index];
            if(element.description == '有'){
              arr.push(element);
            }
          }
          _this.setData({
            configCont: arr
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 获取租车门店商家保障
  _queryTravelVehicleSafeConfig(){
    let _this = this;
    queryTravelVehicleSafeConfig({vehicleId: _this.data.item.id,storeId: _this.data.store.id,isDefault: 1})
    .then(res => {
      if(res.success){
        let fee = 0;
        if(res.data.content.length > 0){
          fee = res.data.content[0].fee;
          _this._queryTravelVehicleStoreDetails({safeguardConfigIds: res.data.content[0].id});
        }
        // _this.setData({
        //   price: 
        // })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 获取租车门店详情
  _queryTravelVehicleStoreDetails({safeguardConfigIds}){
    let _this = this;
    queryTravelVehicleStoreDetails({storeConfigId: _this.data.store.config.id,safeguardConfigIds: safeguardConfigIds,startTime: _this.data.rentCarStartYear + '-' + _this.data.rentCarStartDate + ' ' + _this.data.rentCarStartTime,endTime: _this.data.rentCarEndYear + '-' + _this.data.rentCarEndDate + ' ' + _this.data.rentCarEndTime})
    .then(res => {
      if(res.success){
        _this.setData({
          priceConfig: res.data,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 费用明细
  changeShow(){
    this.setData({
      showScheduleFee: !this.data.showScheduleFee,
    })
  },
  // 去付款
  toSubmitOrder(){
    const { province , city , country , town , detailedAddress , item , store } = this.data;
    wx.navigateTo({
      url: `/pages/Order/SubmitVehicleOrder/SubmitVehicleOrder?province=${province}&city=${city}&country=${country}&town=${town}&detailedAddress=${detailedAddress}&item=${JSON.stringify(item)}&store=${JSON.stringify(store)}`,
    })
  },
})